package com.coollang.smartbra.presenter2;

import android.util.Log;

import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.http.HttpCallBack;
import com.coollang.smartbra.http.MyHttp;
import com.coollang.smartbra.interfaces.ActionType;
import com.coollang.smartbra.interfaces.ShareActionView;
import com.coollang.smartbra.model.ActionDataModel;
import com.coollang.smartbra.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.realm.Realm;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: Timmy
 * @data: 2017/12/16
 * @Fuction:
 */

public class ShareActionPresenter extends AbstractMvpPresenter<ShareActionView> {

    private static final int CORRECT_FLAG = 1;
    private static final int ERROR_FLAG = 2;
    private static final int ERROR_TYPE = 3;

    public static final int PBZC_TYPE = 3;

    private int mActionType;
    private int mCorrectCount;
    private int mCorrectTime;
    private List<Integer> mHeartList;

    /**
     * 解析数据
     */
    public void parseData(ArrayList<NewActionData> actionDatas) {
        mCorrectCount = 0;
        mCorrectTime = actionDatas.get(0).getTime() / 1000;
        mHeartList = new ArrayList();

        for (NewActionData actionData : actionDatas) {
            mActionType = actionData.getActionType();
            int actionType = actionData.getActionFlag();
            mHeartList.add(actionData.getHeartRate());
            if (mActionType == PBZC_TYPE) {
                if (actionType == CORRECT_FLAG) {
                    // mCorrectTime += actionData.getTime();
                }
            } else {
                if (actionType == CORRECT_FLAG) {
                    mCorrectCount++;
                }
            }
        }
    }

    /**
     * @return 当前动作类型
     */
    public int getActionType() {
        return mActionType;
    }

    public int getCorrectTime() {
        return mCorrectTime;
    }

    /**
     * 动作正确个数
     */
    public int getCorrectCount() {
        return mCorrectCount;
    }

    public void saveActionData(ArrayList<NewActionData> actionDatas) {
        NewActionData actionData = actionDatas.get(0);

        ActionDataModel model = new ActionDataModel();
        model.setId(System.currentTimeMillis());

        // 心率
        // 心率最大值
        model.setMax_rate(Collections.max(mHeartList));
        // 心率最小值
        model.setMin_rate(Collections.min(mHeartList));
        // 求平均值
        model.setAve_heart_rate(getHeartAve());
        model.setStart_time(actionData.getStartTime());
        model.setEnd_time(actionDatas.get(actionDatas.size() - 1).getStartTime());

        // 动作
        int type = Constants.ACTION_NAME_ID1[actionData.getActionType()];
        model.setType(type);

        if (Constants.ACTION_NAME_ID1[actionData.getActionType()] == ActionType.PBZC) {
            // 平板支撑时间
            model.setNums(mCorrectTime);
            addTimingAction(model);
        } else {
            // 正确的动作个数
            model.setNums(mCorrectCount);
            // 错误个数
            model.setError_Nums(actionDatas.size() - mCorrectCount);
            addNumberAction(model);
        }
    }

    /**
     * 求心率平均值
     */
    private int getHeartAve() {
        int sum = 0;
        int size = mHeartList.size();
        for (int i = 0; i < size; i++) {
            int number = mHeartList.get(i);
            sum += number;
        }
        return sum / size;
    }

    /**
     * 添加计时运动
     *
     * @param model
     */
    private void addTimingAction(final ActionDataModel model) {
        Map<String, String> map = new HashMap<>(16);

        Log.i("gzw", "sport_duration: " + String.valueOf(model.getNums()));

        map.put(Constants.TYPE, String.valueOf(model.getType()));
        map.put("sport_duration", String.valueOf(model.getNums()));
        map.put("ave_heart_rate", String.valueOf(model.getAve_heart_rate()));
        map.put("max_heart_rate", String.valueOf(model.getMax_rate()));
        map.put("min_heart_rate", String.valueOf(model.getMin_rate()));
        map.put("start_time", String.valueOf(model.getStart_time() / 1000));
        map.put("end_time", String.valueOf(model.getEnd_time() / 1000));

        MyHttp.getInstance().addTimingAction(map, new HttpCallBack() {
            @Override
            public void onSuccess(String results, Call call, Response response) {
                Realm.getDefaultInstance().beginTransaction();
                Realm.getDefaultInstance().copyToRealm(model);
                Realm.getDefaultInstance().commitTransaction();
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                ToastUtils.showToast("网络异常,上传数据失败");
            }
        });

    }

    /**
     * 添加计数运动
     */
    private void addNumberAction(final ActionDataModel model) {
        Map<String, String> map = new HashMap<>(16);
        map.put(Constants.TYPE, String.valueOf(model.getType()));
        map.put("amount", String.valueOf(model.getNums()));
        map.put("fault", String.valueOf(model.getNums()));
        map.put("start_time", String.valueOf(model.getStart_time() / 1000));
        map.put("end_time", String.valueOf(model.getEnd_time() / 1000));
        map.put("sport_duration", String.valueOf((model.getEnd_time() - model.getStart_time())));
        map.put("ave_heart_rate", String.valueOf(model.getAve_heart_rate()));
        map.put("max_heart_rate", String.valueOf(model.getMax_rate()));
        map.put("min_heart_rate", String.valueOf(model.getMin_rate()));

        MyHttp.getInstance().addNumberAction(map, new HttpCallBack() {

            @Override
            public void onSuccess(String results, Call call, Response response) {
                Realm.getDefaultInstance().beginTransaction();
                Realm.getDefaultInstance().copyToRealm(model);
                Realm.getDefaultInstance().commitTransaction();
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                ToastUtils.showToast("网络异常,上传数据失败");
            }
        });
    }
}
